package com.tianqu.tms.www.user.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserClass;
import com.tianqu.tms.www.user.viewhelper.UserDetailViewHelper;

/**
 * 修改用户信息录入页面
 * 
 * @author tianshufu
 * 
 */
public class UserUpdateInputController extends TmsController {

	private Logger logger = Logger.getLogger(UserUpdateInputController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#doAction was called.");

		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String uniquId = (String) uc.getAttribute("uniqueId");
			// 根据主键ID查询出用户信息 放到viewhelp中
			UserDetailViewHelper detalviewhelper = new UserDetailViewHelper();

			User entity = new User();
			entity = entity.getUserInfoById(Long.parseLong(uniquId));

			setViewHelperFromEntity(detalviewhelper, entity);

			UserClass code = UserClass.getInstance();

			detalviewhelper.setUserTypeList(code.getCreatUserList());

			uc.setAttribute(TmsConstants.VIEWHELP, detalviewhelper);// 将viewhelp的值放到uc中

			logger
					.debug(this.getClass().getName()
							+ "#doAction was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 将entity中的值存如viewhelper中
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public UserDetailViewHelper setViewHelperFromEntity(
			UserDetailViewHelper view, User entity) throws TmsDaoException {
		view.setId(String.valueOf(entity.getId()));
		view.setLoginName(entity.getLoginId());
		view.setUserName(entity.getRealName());
		// 从字典表中查出用户类型信息
		view.setUserType(entity.getUcId());
		return view;
	}

}
